<!DOCTYPE html>
<html lang="en"  xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>修改用户信息</title>
    <link rel="stylesheet" th:href="@{~/component/pear/css/pear.css}" media="all">
    <link rel="stylesheet" th:href="@{~/common/css/common.css}" media="all">
</head>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
            	<input type="hidden" id="id" name="id" >
                <div class="layui-form-item">
                    <label class="layui-form-label">所属部门</label>
                    <div class="layui-input-block">
                    	<ul id="deptTree"  class="dtree" data-id="0"></ul>
                        <!-- <input id="deptId" type="text" style="display:none" name="deptId" class="layui-input">
                        <input id="deptName" type="text" readonly="readonly"  autocomplete="off" placeholder="请选择部门" class="layui-input"> -->
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">用户角色</label>
                    <div  class="layui-input-block ">
                        <select id="roleIds" name="roleIds"  xm-select>
                        	<option value='' disabled style='display:none;'>请选择角色</option>
	            			<!-- <option value="1" >超级管理员</option>
	            			<option value="2" >管理员</option>
	            			<option value="3">普通用户</option>  -->
	            		</select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">用户账号</label>
                    <div class="layui-input-block">
                        <input type="text" id="username" lay-verify="required" name="username" autocomplete="off" placeholder="请输入用户账号" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">用户名称</label>
                    <div class="layui-input-block">
                        <input type="text" id="nickname" lay-verify="required" name="nickname" autocomplete="off" placeholder="请输入用户名称" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">用户电话</label>
                    <div class="layui-input-block">
                        <input type="text" id="phone" name="phone" lay-verify="phone" autocomplete="off" placeholder="请输入用户电话" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">用户性别</label>
                    <div class="layui-input-block">
                        <input type="radio" name="sex" value="1" title="男" checked>
                        <input type="radio" name="sex" value="0" title="女">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">账号状态</label>
                    <div class="layui-input-block" >
                        <input type="radio" name="status" value="1" title="正常" checked>
                        <input type="radio" name="status" value="0" title="禁用">
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button id="submitBtn" type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="user-save">
                <i class="layui-icon layui-icon-release"></i> 确认修改
            </button>
            <button type="button" id="close" class="pear-btn pear-btn-danger pear-btn-sm" >
                <i class="layui-icon layui-icon-close"></i> 取消
            </button>
        </div>
    </div>
</form>
<script th:src="@{~/component/layui/layui.js}" charset="utf-8"></script>
<script th:src="@{~/component/pear/pear.js}" charset="utf-8"></script>
<script th:src="@{~/common/js/common.js}" charset="utf-8"></script>
<script>
layui.use(['form','jquery','dtree','select'],function(){
    let form = layui.form
    	,dtree = layui.dtree
    	,select = layui.select;
    let $ = layui.jquery;
    
    let id = getQueryString("id");
    
    $.post("/user/selectById",{"id":id},function(result){
        if(result.code == 0){
        	$("#id").val(result.data.id);
        	$("#username").val(result.data.username);
        	$("#nickname").val(result.data.nickname);
        	$("#phone").val(result.data.phone);
        	$("input[name=sex][value=1]").attr("checked", result.data.sex == 1 ? true : false);
            $("input[name=sex][value=0]").attr("checked", result.data.sex == 0 ? true : false);
        	$("input[name=status][value=1]").attr("checked", result.data.status == 1 ? true : false);
            $("input[name=status][value=0]").attr("checked", result.data.status == 0 ? true : false);
            form.render(); //更新全部   
            /* 调用渲染部门树形选择框方法 */
            dtreeRender(result.data.deptId);
            /* 调用渲染角色下拉选择框方法 */
            selectRender(result.data.roleList);
        }else{
            layer.msg(result.msg,{icon:2,time:3000});
        }
	});

    /* 点击确认修改按钮提交数据 */
    form.on('submit(user-save)', function(data){
        $.ajax({
            url:'/user/update',
            data:JSON.stringify(data.field),
            dataType:'json',
            contentType:'application/json',
            type:'post',
            success:function(result){
                if(result.code == 0){
                    layer.msg(result.msg,{icon:1,time:1000},function(){
                        parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                        parent.layui.table.reload("user-table");
                    });
                }else{
                    layer.msg(result.msg,{icon:2,time:3000});
                }
            }
        }) 
        return false;
    });
    
    /* 定义渲染部门树形选择框方法 */
    var dtreeRender = function(deptId) {
	    dtree.render({
	        elem: "#deptTree",
	        initLevel: "1",
	        method: 'get',
	        selectTips: "请选择部门",
	        ficon: ["1", "-1"],
	        icon:"2",
	        selectInputName: {
	          nodeId: "deptId",
	          context: "deptName"
	        },
	        /* url: "../../../../admin/data/dtree.json", */
	        dataStyle: "layuiStyle",
        	url: "/dept/selectDeptTree",
        	response:{message:"msg",statusCode:0},
	        done: function(res){
	            dtree.dataInit("deptTree", deptId);
	            dtree.selectVal("deptTree");
	          
	        },
	        select: true
	    });
    }
    
    /** 渲染下拉框多选框**/
    window.selectRender = function(roleList){
    	var roleIds = [];
    	$.each(roleList,function(index,item) {
    		roleIds.push(item.id);
    	})
	    $.get("/role/selectList",{"status":"1"},function(result){
	    	if(result.code == 0){
	    		for(var i = 0; i < result.data.length; i++){
	    			var id = result.data[i].id;
	    			var name = result.data[i].name;
	    			var selectd = $.inArray(id, roleIds) >= 0 ? true : false;
		    		$('#roleIds').append(new Option(name,id,selectd,selectd));
	    		}
	    		select.render('roleIds');
	    	}else{
	    		layer.msg(result.msg);
	    	}
	    });
    }
    
    $("#close").click(function(){
    	parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
    });
    
    
})




</script>
</body>
</html>